import numpy as np
from numpy.matlib import repmat

def kernel_gip(y_train, dim, gamma):
    y = y_train
    y = np.mat(y)
    if dim == 1:
        ga = y * y.T
    else:
        ga = y.T * y
    ga = gamma * ga / np.mean(np.diag(ga))
    d = np.exp(-kernel2distance(ga))
    return d


def kernel2distance(k):
    di = np.diag(k)
    d1 = repmat(di, len(k), 1)
    d2 = d1.T
    d = d1 + d2 - 2 * k
    return d
